Home:ALL Converter>include STL header file inside a function C++14

include STL header file inside a function C++14

Ask Time:2018-06-16T02:31:57         Author:madhur4127

Json Formatter

tl;dr: Can I somehow make this code work in C++14 (GCC 6.3)?

int main(){
    #include<vector>
    std::vector<int> v{1,2,3};
    return 0;
}

But code below works just fine!

#include <iostream>
using namespace std;

int main() {
    #include<cstdio>
    using namespace __gnu_cxx;
    printf("Hello world\n.");
    return 0;
}

Using C++14 (gcc-6.3) code doesn't compile with error message being

 error: 'namespace' definition is not allowed here
 namespace std
 ^~~~~~~~~

Why I want to do this?

I don't have access outside of the function where I am allowed to code. I can't #include in global area.

UPD: Changing to cstdlib also works problem is not exclusion by header guard (according to me) but namespace problem. Because C++ header files have namespace std, while c header files doesn't. I wanted to ask whether there is some tweak for namespace issue?

Author:madhur4127,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/50881008/include-stl-header-file-inside-a-function-c14
yy